package com.artron.mediaartron.ui.fragment.login;

import android.os.Handler;
import android.support.v4.app.Fragment;


import com.artron.baselib.base.BaseFragment;
import com.artron.baselib.base.BaseLoadingFragment;
import com.artron.baselib.base.BaseStaticFragment;
import com.artron.mediaartron.R;
import com.artron.mediaartron.data.config.AppProfile;


/**
 * Created by Doing on 2016/9/27.
 *
 */
public class LoginWrapperFragment extends BaseStaticFragment {

    private BaseFragment mContentFragment;

    public static LoginWrapperFragment newInstance(BaseFragment contentFragment) {
        LoginWrapperFragment fragment = new LoginWrapperFragment();
        fragment.init(contentFragment);
        return fragment;
    }

    public void init(BaseFragment contentFragment) {
        mContentFragment = contentFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_login_wrapper;
    }

    @Override
    protected void initView() {
        if (mContentFragment == null) {
            return;
        }

        if (AppProfile.getUserInfo().isLogin()) {
            if (!mContentFragment.isAdded()) {
                replaceFragment(mContentFragment);
            }
        }else{
            replaceFragment(NoLoginFragment.newInstance());
        }
    }

    private void replaceFragment(Fragment fragment) {
        getChildFragmentManager()
                .beginTransaction()
                .replace(R.id.LoginWrapperFragment_root, fragment)
                .commitAllowingStateLoss();
    }

    public void resetView() {
        initView();
        if (AppProfile.getUserInfo().isLogin()) {
            if (mContentFragment instanceof BaseLoadingFragment) {
                new Handler().post(new Runnable() {
                    @Override
                    public void run() {
                        ((BaseLoadingFragment) mContentFragment).showImmediate();
                    }
                });
            }
        }
    }

    public BaseFragment getContentFragment() {
        return mContentFragment;
    }
}
